package com.nya.roulette;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.widget.ImageView;

// Class ImageViewCanvas
// Background image of roulette layout. Used to draw chips on.
public class ImageViewCanvas extends ImageView implements Serializable {

	private static final long serialVersionUID = 1L;
	private ArrayList<Chip> chips;
	
	public ImageViewCanvas(Context context, AttributeSet attrs) {
		super(context, attrs);
		chips = new ArrayList<Chip>();
	}
	
	public void addChip(Chip chip) {
		chips.add(chip);
	}
	
	public void removeChips() {
		chips.clear();
	}

	@Override
	public void onDraw(Canvas canvas) {
		
		// Iterate through all chips and draw them
		Iterator<Chip> chipI = chips.iterator();
		while(chipI.hasNext()) {
			Chip chip = chipI.next();
			drawChip(chip.getValue(), chip.getLocation(), canvas);
		}
	}
	
	public void drawChip(int value, Point location, Canvas canvas) {
		
		int radius = 18;
		int fontSize = radius * 2;
		
		Paint paint = new Paint();
		paint.setStyle(Paint.Style.FILL);
		paint.setTextSize(fontSize);
		paint.setColor(Color.WHITE);
		
		canvas.drawCircle(location.x + radius / 2 + 1, location.y - radius / 2 - 4, radius, paint);
		paint.setColor(Color.BLACK);
		canvas.drawText(Integer.toString(value), location.x, location.y, paint);
	}
}